home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / lib / mathlib / psort / test / speed.c < prev   
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.6 KB  |  240 lines

  1. /******************************************************************************
  2. *                                          *
  3. * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.              *
  4. * All Rights Reserved.                                  *
  5. *                                          *
  6. * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;      *
  7. * the contents of this file may not be disclosed to third parties, copied or  *
  8. * duplicated in any form, in whole or in part, without the prior written      *
  9. * permission of Silicon Graphics, Inc.                          *
  10. *                                          *
  11. * RESTRICTED RIGHTS LEGEND:                              *
  12. * Use, duplication or disclosure by the Government is subject to restrictions *
  13. * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data      *
  14. * and Computer Software clause at DFARS 252.227-7013, and/or in similar or    *
  15. * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -     *
  16. * rights reserved under the Copyright Laws of the United States.          *
  17. *                                          *
  18. *******************************************************************************
  19. *                                          *
  20. *    PSORT written by                              *
  21. *                Jean-Pierre Panziera                  *
  22. *                            jpp@corp.sgi.com      *
  23. *                                          *
  24. **************************************************************************** */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <math.h>
  28.  
  29. #include "psort.h"
  30.  
  31. #define MIN(a,b) ( ((a) < (b)) ? (a) : (b))
  32. #define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
  33.  
  34. #define MAX_SIZE    111111
  35. #define N_MIN        100000
  36.  
  37. void (*ref_sort)();
  38. void (*this_sort)();
  39.  
  40. extern void merge_sort();
  41.  
  42. double second();
  43.  
  44.  
  45. int this_compar( a, b)
  46. int *a, *b;
  47. {
  48.     if( (*a) < (*b)) 
  49.     return (-1);
  50.     if( (*a) > (*b))
  51.     return (1);
  52.     return (0);
  53. }
  54.  
  55. int min_size, max_size;
  56. int n_total;
  57. double inc_size;
  58. int is_speedup;
  59.  
  60. main( argc, argv)
  61.      int argc;
  62.      char *argv[];
  63. {
  64.     int log_size;
  65.     int i,j,size, iseed, itmp, this_max;
  66.     int *int_array, *int_copy;
  67.  
  68.     double t, x;
  69.  
  70.     GetArguments();
  71.     srandom(getpid());
  72.     int_array = (int  *) malloc( (n_total+1) * sizeof(int  ));
  73.     int_copy  = (int  *) malloc( (n_total+1) * sizeof(int  ));
  74.  
  75.     for ( size = min_size ; size <= max_size ; 
  76.     size = ( ( (i=(int)((double)size * inc_size)) > size) ? i : (size+1) ) ) {
  77. /*
  78. ************************************************
  79.     setting up the arrays
  80. ************************************************
  81. */
  82.      this_max = MIN(N_MIN,n_total);
  83.      t = second();
  84.  
  85.     for( i = 0 ; i < this_max ; i++) {
  86.         int_array [i] = (random() % (2* size));
  87.     }
  88.     int_array[this_max] = -1;
  89.  
  90. #pragma parallel 
  91. #pragma shared(size, this_max, int_array, int_copy)
  92. #pragma local(i)
  93. {
  94. #pragma pfor iterate(i=0;(this_max+1);1)
  95.     for (i = 0 ; i < (this_max+1) ; i++) {
  96.         int_copy[i] = int_array[i];
  97.     }
  98. }
  99.       t = second() - t;
  100.  
  101.     printf("%10d ", size);
  102. /*
  103. ************************************************
  104.     p_sorting the array #2
  105. ************************************************
  106. */
  107.       t = second();
  108.  
  109.     for( i = 0; i <= (this_max-size) ; i += size)
  110.         this_sort( (int_copy+i), size, sizeof(int), this_compar);
  111.  
  112.       t = second() - t;
  113.  
  114. /*
  115. ************************************************
  116.     q_sorting the array #1
  117. ************************************************
  118. */
  119.     if( is_speedup) { 
  120.       x = second();
  121.  
  122.     for( i = 0; i <= (this_max-size) ; i += size)
  123.         ref_sort( (int_array+i), size, sizeof(int), this_compar);
  124.  
  125.       x = second() - x;
  126.  
  127.       x = (x > 0.  && t> 0.) ? (x / t) : 0.0;
  128.  
  129.     }
  130.     else {
  131.     for ( i = 2, log_size = 1 ; i <= size ; i *= 2, log_size ++){ }
  132.     x = (t>0) ? (1.0e-6 * (double)(size*(this_max/size)) * (double)(log_size) / t) : 0.0;
  133.     }
  134.     printf("%12.3f \n",x);
  135. /*
  136. ************************************************
  137.     checking the sorted values
  138. ************************************************
  139. */
  140.     if( int_array[this_max] != -1) {
  141.         printf("Outside value modified \n");
  142.         exit (-1);
  143.     }
  144. /*
  145. ************************************************
  146. */
  147.     }
  148.     free(int_array);
  149.     free(int_copy);
  150. }
  151.  
  152. #include <sys/time.h>
  153. /*  #include <sys/resource.h>  */
  154.  
  155. /* **********************************************************************
  156.  
  157.    give the elapsed wall clock time
  158.  
  159. ********************************************************************** */
  160. double second()
  161. {
  162.         struct timeval s_val;
  163.     struct timezone s_z;
  164.  
  165.     static long zero_sec = 0;
  166.     static long zero_usec = 0;
  167.     long n_sec, n_usec;
  168.  
  169.         gettimeofday(&s_val, &s_z);
  170.  
  171.     n_sec = s_val.tv_sec;
  172.     n_usec = s_val.tv_usec;
  173.     if( zero_sec == 0 ) {
  174.          zero_sec = n_sec;
  175.         zero_usec = n_usec;
  176.     }
  177.  
  178.     n_sec = n_sec - zero_sec;
  179.     n_usec = n_usec - zero_usec;
  180.  
  181.     return( (double) n_sec +  ( (double) n_usec * 1.0E-6 ) );
  182. }
  183.  
  184. GetArguments( argc, argv)
  185. int argc;
  186. char *argv[];
  187. {
  188.     int i, j, k;
  189.     int nerror = 0;
  190.  
  191.     is_speedup = 0;
  192.     srand( 0x01 | (123 * getpid()));
  193.  
  194.     min_size = 2;
  195.     max_size = MAX_SIZE;
  196.     inc_size = 2.0;
  197.  
  198.    this_sort = qsort;
  199.     ref_sort  = NULL;
  200. /* ******************************************************* */
  201.     for ( i = 1 ; (i < argc) && (nerror != 1) ; i ++ ) {
  202.     if( argv[i][0] == '-') {
  203.         switch ( argv[i][1]) {
  204.         case 'p' :
  205.         case 'P' :  
  206.                 this_sort = psort;
  207.                 break;
  208.         case 'x' :
  209.         case 'X' :  
  210.                 this_sort = psort;
  211.                 ref_sort = qsort;
  212.                 is_speedup = 1;
  213.                 break;
  214.         default  :  nerror = 1;
  215.         }
  216.     }
  217.     else {
  218.         if( i+3 > argc)
  219.         nerror = 1;
  220.         else { 
  221.         min_size  = atoi( argv[i+0]);
  222.         max_size  = atoi( argv[i+1]);
  223.         inc_size  = atof( argv[i+2]);
  224.         i += 3;
  225.         i --;
  226.         }
  227.     }
  228.     }
  229.     n_total = MAX( max_size, N_MIN);   
  230. /*
  231.     printf("n_total : %d\n",n_total);
  232. */
  233.     if( nerror == 1) {
  234.     fprintf( stderr, 
  235.         "Usage : %s [-p[arallel]] [-x(speedup)] [minsize maxsize step]\n", 
  236.         argv[0]);
  237.     exit(-1);
  238.     }
  239. }
  240.